package com.dongdongshop.controller;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.*;
import com.dk.data.R;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.SpecificationService;
import com.dongdongshop.service.TbgoodService;
import com.dongdongshop.service.TbtypeService;
import com.dongdongshop.service.TicatService;
import com.dongdongshop.vo.SpecJsonVo;
import com.dongdongshop.vo.SpecVo;
import com.dongdongshop.vo.TytypeTemoplateVO;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

//增加商品信息
@Controller
@RequestMapping("good")
public class TbgoodController {


    @DubboReference
    TicatService ts;//分类表三级联动


    @DubboReference
    TbtypeService tys;  //模板表

    @DubboReference
    SpecificationService sfs;//规格

    @DubboReference
    TbgoodService tgs;//商品表


    @RequestMapping("tiao")
    public String tiao(){
        return "admin/goods_edit";
    }


    //增加数据
    @RequestMapping("insertgood")
    @ResponseBody
    public R insertgood(TbGoods tg , TbGoodsDesc tgd, String Stringguige ){
        List<TbItem> tbitem = JSONObject.parseArray(Stringguige, TbItem.class);
        tgs.insertgood(tg,tgd,tbitem);
        return R.ok();
    }




    //三级联动查询
    @RequestMapping("getthree")
    @ResponseBody
    public R<List<TbItemCat>> getthree(Long parentId){
        List<TbItemCat> ls = ts.getsanji(parentId);
        return R.ok().setData(ls);
    }

    //三级联动,返回模板id
    @RequestMapping("getbyid")
    @ResponseBody
    public R<TbItemCat> getbyid(Long id){
        TbItemCat tb = ts.getbyid(id);
        return R.ok().setData(tb);
    }


    //模板表根据id查询数据
    @RequestMapping("gettypeid")
    @ResponseBody
    public R gettypeid(Long id)  {
        TbTypeTemplate type = tys.gettypeid(id);

        TytypeTemoplateVO typevo=new TytypeTemoplateVO();
//        BeanUtils.copyProperties(type,typevo);//将type的属性赋值给vo类
        typevo.setId(type.getId());
        typevo.setName(type.getName());
        typevo.setBrandIds(type.getBrandIds());
        typevo.setCustomAttributeItems(type.getCustomAttributeItems());



        String specIds = type.getSpecIds();//[{"id":"35","text":"大的规格"},{"id":"36","text":"小的规格"}]
        //将json转换为list类型
        //将json类型的specids数据,转换成泛型为specjsonvo的list集合
        List<SpecJsonVo> specJsonVos = JSONObject.parseArray(specIds, SpecJsonVo.class);
        for (SpecJsonVo  spec: specJsonVos) {
            SpecVo specvo = sfs.findSpecbyid(spec.getId());
            spec.setOptionList(specvo.getOptionList());//将规格选项集合放入vo类中
        }
        typevo.setSpecvoslist(specJsonVos);
        return R.ok().setData(typevo);
    }



    //图片上传
    @RequestMapping("upload")
    @ResponseBody
    public R upload(@RequestParam("image") MultipartFile image){
        String http="https://";

        // yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1（杭州）为例，Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
        String endpoint = "oss-cn-beijing.aliyuncs.com";
        // 阿里云账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维，请登录RAM控制台创建RAM用户。
        String accessKeyId ="LTAI5tJG9udR6TFeyPkPiSxb" ;
        String accessKeySecret = "UG5PcFu7GxGDMAKm4W7qOeURpBIzfn";
        // 填写Bucket名称，例如examplebucket。
        String bucketName = "dk2202-w";
        String imagedir="image/";
        // 填写文件名。文件名包含路径，不包含Bucket名称。例如exampledir/exampleobject.txt。
        String objectName = imagedir+image.getOriginalFilename();
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(http+endpoint, accessKeyId, accessKeySecret);
        try {
            ossClient.putObject(bucketName, objectName, image.getInputStream() );
        }catch (Exception oe){
        }finally {
            if(ossClient !=null ){
                ossClient.shutdown();// 关闭OSSClient。
            }
        }
//        https://dk2202-w.oss-cn-beijing.aliyuncs.com/image/c_0001.jpg
        StringBuffer url=new StringBuffer();
        url.append(http);
        url.append(bucketName);
        url.append(".");
          url.append(endpoint);
          url.append("/");
          url.append(objectName);
          String s=url.toString();//将上传成功的路径返回前台
        return R.ok().setData(s);
    }




}
